#include "board_oled.h"

uint8_t show_tick1ms;
static uint16_t show_cnt;
#define SHOW_CNT 100			//刷新间隔100ms
T_oled_show_t oled_show_t = {
	.status = 0,
	.show_refresh_num = 0,
	.logo_cnt = 0,
	.ver_cnt = 0
};
#define CHAR_FLASH 4	//���Ƶ��

static void oled_show_param(void)
{
	if(device_t.status.work_sta==STATUS_WORK)		//工作状态
	{
		if(device_t.mode_sum==0)
		{
			OLED_ShowString816(5,0,(unsigned char*)"CCT");			
			
			OLED_DrawBMP_sene_816(40,3,1,1);
			OLED_ShowNum816(68,3,device_t.show_k,4,16);     //KEY_DAT     device_t.show_k  xin
			OLED_ShowString816(100,3,(unsigned char*)"K");
			
			OLED_DrawBMP_sene_816(40,6,0,1);    			//̫太阳
			OLED_ShowNum816(76,6,device_t.light_sum,3,16);
			OLED_ShowString816(100,6,(unsigned char*)"%");
			
			if(device_t.flag_val==0)
			{
				OLED_DrawBMP_sene_816(24,6,3,1);
				OLED_DrawBMP_sene_816(24,3,2,1);
			}
			else
			{
				OLED_DrawBMP_sene_816(24,3,3,1);
				OLED_DrawBMP_sene_816(24,6,2,1);
			}
		}
		else if(device_t.mode_sum==1)
		{
			OLED_ShowString816(5,0,(unsigned char*)"RGB");
			
			OLED_ShowString816(46,2,(unsigned char*)"H");			
			OLED_ShowNum816(76,2,device_t.rgb_h,3,16);			
			OLED_ShowCHinese16(100,2,0);
			
			OLED_ShowString816(46,4,(unsigned char*)"S");
			OLED_ShowNum816(76,4,device_t.rgb_s,3,16);
			
			OLED_DrawBMP_sene_816(40,6,0,1);
			OLED_ShowNum816(76,6,device_t.light_sum,3,16);
			OLED_ShowString816(100,6,(unsigned char*)"%");
			
			if(device_t.flag_val==0)
			{
				if(device_t.rgb_val==0)
				{
					OLED_DrawBMP_sene_816(24,6,3,1);
					OLED_DrawBMP_sene_816(24,4,3,1);
					OLED_DrawBMP_sene_816(24,2,2,1);
				}
				else
				{
					OLED_DrawBMP_sene_816(24,6,3,1);
					OLED_DrawBMP_sene_816(24,2,3,1);
					OLED_DrawBMP_sene_816(24,4,2,1);
				}
			}
			else
			{
				OLED_DrawBMP_sene_816(24,2,3,1);
				OLED_DrawBMP_sene_816(24,4,3,1);
				OLED_DrawBMP_sene_816(24,6,2,1);
			}
		}
		else if(device_t.mode_sum==2)
		{
			OLED_ShowString816(2,0,(unsigned char*)"SCENE:");
			#ifdef RGBWY
			OLED_ShowNum816(50,0,device_t.sence,2,16);
			#else
			switch(device_t.sence)
			{
				case 4: OLED_ShowNum816(50,0,1,1,16);break;
				case 6: OLED_ShowNum816(50,0,2,1,16);break;
				case 7: OLED_ShowNum816(50,0,3,1,16);break;
				case 9: OLED_ShowNum816(50,0,4,1,16);break;
				case 11: OLED_ShowNum816(50,0,5,1,16);break;
				case 13: OLED_ShowNum816(50,0,6,1,16);break;
				case 14: OLED_ShowNum816(50,0,7,1,16);break;
				case 15: OLED_ShowNum816(50,0,8,1,16);break;					
				default:break;
			}
			#endif
			mode_3_data(device_t.sence);	
			
			OLED_DrawBMP_sene_816(40,6,0,1);
			OLED_ShowNum816(76,6,device_t.light_sum,3,16);
			OLED_ShowString816(100,6,(unsigned char*)"%");

			if(device_t.flag_val==0)
			{
				OLED_DrawBMP_sene_816(24,6,3,1);
			}
			else
			{
				OLED_DrawBMP_sene_816(24,6,2,1);
			}
		}			
	}
	switch(device_t.bat_lev_show)
	{
		case 0: OLED_DrawBMP(100,0,124,2,BMP0);break;
		case 1: OLED_DrawBMP(100,0,124,2,BMP1);break;
		case 2: OLED_DrawBMP(100,0,124,2,BMP2);break;
		case 3: OLED_DrawBMP(100,0,124,2,BMP3);break;
		case 4: OLED_DrawBMP(100,0,124,2,BMP4);break;
		default:break;
	}
}
static void show_val(void)
{
	if(device_t.mode_sum==0)
	{
		if(device_t.flag_val==0)
		{
			OLED_DrawBMP_sene_816(24,6,3,1);
			OLED_DrawBMP_sene_816(24,3,2,1);
		}
		else
		{
			OLED_DrawBMP_sene_816(24,3,3,1);
			OLED_DrawBMP_sene_816(24,6,2,1);
		}
	}
	else if(device_t.mode_sum==1)
	{
		if(device_t.flag_val==0)
		{
			if(device_t.rgb_val==0)
			{
				OLED_DrawBMP_sene_816(24,6,3,1);
				OLED_DrawBMP_sene_816(24,4,3,1);
				OLED_DrawBMP_sene_816(24,2,2,1);
			}
			else
			{
				OLED_DrawBMP_sene_816(24,6,3,1);
				OLED_DrawBMP_sene_816(24,2,3,1);
				OLED_DrawBMP_sene_816(24,4,2,1);
			}
		}
		else
		{
			OLED_DrawBMP_sene_816(24,2,3,1);
			OLED_DrawBMP_sene_816(24,4,3,1);
			OLED_DrawBMP_sene_816(24,6,2,1);
		}
	}
	else if(device_t.mode_sum==2)
	{
		if(device_t.flag_val==0)
		{
			OLED_DrawBMP_sene_816(24,6,3,1);
		}
		else
		{
			OLED_DrawBMP_sene_816(24,6,2,1);
		}
	}	
}
static void oled_show_change(uint8_t len)
{
	static uint8_t cnt;
	
	switch(len)
	{
		case NNUM:
			break;
		case KNUM:
			OLED_ShowNum816(68,3,device_t.show_k,4,16);
			break;
		case KLNUM:
			OLED_ShowNum816(76,6,device_t.light_sum,3,16);
			break;
		case HNUM:
			OLED_ShowNum816(76,2,device_t.rgb_h,3,16);
			break;
		case SNUM:
			OLED_ShowNum816(76,4,device_t.rgb_s,3,16);
			break;
		case ILNUM:
			OLED_ShowNum816(76,6,device_t.light_sum,3,16);
			break;
		case SENNUM:
			#ifdef RGBWY
			OLED_ShowNum816(50,0,device_t.sence,2,16);
			#else
			switch(device_t.sence)
			{
				case 4: OLED_ShowNum816(50,0,1,1,16);break;
				case 6: OLED_ShowNum816(50,0,2,1,16);break;
				case 7: OLED_ShowNum816(50,0,3,1,16);break;
				case 9: OLED_ShowNum816(50,0,4,1,16);break;
				case 11: OLED_ShowNum816(50,0,5,1,16);break;
				case 13: OLED_ShowNum816(50,0,6,1,16);break;
				case 14: OLED_ShowNum816(50,0,7,1,16);break;
				case 15: OLED_ShowNum816(50,0,8,1,16);break;					
				default:break;
			}
			#endif
			OLED_Clear(3,5);
			mode_3_data(device_t.sence);
			break;
		case STRNUM:
			#ifdef RGBWY
			OLED_ShowNum816(50,0,device_t.sence,2,16);
			#else
			switch(device_t.sence)
			{
				case 4: OLED_ShowNum816(50,0,1,1,16);break;
				case 6: OLED_ShowNum816(50,0,2,1,16);break;
				case 7: OLED_ShowNum816(50,0,3,1,16);break;
				case 9: OLED_ShowNum816(50,0,4,1,16);break;
				case 11: OLED_ShowNum816(50,0,5,1,16);break;
				case 13: OLED_ShowNum816(50,0,6,1,16);break;
				case 14: OLED_ShowNum816(50,0,7,1,16);break;
				case 15: OLED_ShowNum816(50,0,8,1,16);break;					
				default:break;
			}
			#endif
			OLED_Clear(3,5);
			mode_3_data(device_t.sence);	
			break;
		case SLNUM:
			OLED_ShowNum816(76,6,device_t.light_sum,3,16);
			break;
		case MODENUM:
			OLED_ShowString816(0,0,(uint8_t*)"          ");
			OLED_Clear(2,8);
			oled_show_param();
			break;
		case MODECLO:
			OLED_Clear(0,8);
			OLED_Display_Off();
			SCL_1;
			SDA_1;
			oled_show_t.status=SHOW_NULL;
			device_t.status.show_sta=0;
			break;
		case VOLNUM:
			show_val();
			break;
		case CHARNUM:
			OLED_ShowString816(0,0,(uint8_t*)"          ");
			OLED_Clear(2,8);
			break;
		default:
			break;
	}
	oled_show_t.show_refresh_num=NNUM;
	if(device_t.status.work_sta)
	{
		if(device_t.status.char_sta==1)
		{
			if(device_t.status.flag_full)
			{
				cnt=0;
				OLED_DrawBMP(100,0,124,2,BMP4);
				OLED_ShowString816(84,0,(uint8_t *)"OK");
			}
			else
			{
				cnt++;
				if(cnt<(CHAR_FLASH+1))
				{
					if(device_t.bat_lev_show>=1)
					{
						switch(device_t.bat_lev_show-1)
						{
							case 0: OLED_DrawBMP(100,0,124,2,BMP0);break;
							case 1: OLED_DrawBMP(100,0,124,2,BMP1);break;
							case 2: OLED_DrawBMP(100,0,124,2,BMP2);break;
							case 3: OLED_DrawBMP(100,0,124,2,BMP3);break;
							case 4: OLED_DrawBMP(100,0,124,2,BMP4);break;
							default:break;
						}
					}
					else
					{
						OLED_DrawBMP(100,0,124,2,BMP0);
					}
				}
				else if(cnt<(2*CHAR_FLASH+1))
				{
					if(device_t.bat_lev_show>=1)
					{
						switch(device_t.bat_lev_show)
						{
							case 0: OLED_DrawBMP(100,0,124,2,BMP0);break;
							case 1: OLED_DrawBMP(100,0,124,2,BMP1);break;
							case 2: OLED_DrawBMP(100,0,124,2,BMP2);break;
							case 3: OLED_DrawBMP(100,0,124,2,BMP3);break;
							case 4: OLED_DrawBMP(100,0,124,2,BMP4);break;
							default:break;
						}
					}
					else
					{
						OLED_DrawBMP(100,0,124,2,BMP1);
					}
				}
				if(cnt>=(2*CHAR_FLASH))
				{
					cnt=0;
				}
			}
		}
		else
		{
			cnt=0;
			switch(device_t.bat_lev_show)
			{
				case 0: OLED_DrawBMP(100,0,124,2,BMP0);break;
				case 1: OLED_DrawBMP(100,0,124,2,BMP1);break;
				case 2: OLED_DrawBMP(100,0,124,2,BMP2);break;
				case 3: OLED_DrawBMP(100,0,124,2,BMP3);break;
				case 4: OLED_DrawBMP(100,0,124,2,BMP4);break;
				default:break;
			}
		}
	}
}
/**
  * @}
  */
//=======================================================
static int _get(unsigned char *buffer,int len)
{
	return 0;
}
//灭屏
static int _set(unsigned char *buffer,int len)
{
	OLED_Clear(0,8);
	OLED_Display_Off();
	SCL_1;
	SDA_1;
	oled_show_t.show_refresh_num=NNUM;
	return 0;
}
static int _init(void)
{	
	show_cnt=SHOW_CNT-1;
	if(device_t.status.char_sta==0)
	{
		oled_show_t.status=SHOW_NULL;
	}
	else
	{
		device_t.status.pwm_sta=1;
		oled_show_t.status=SHOW_PARAM;
	}
	oled_show_t.logo_cnt=0;
	oled_show_t.show_refresh_num=NNUM;
	OLED_Init();
	OLED_DrawBMP(0,0,128,8,logo);
	return 0;
}
//
static int _status(unsigned char *buffer,int len)
{
	if(oled_show_t.status==SHOW_CHANGE)
	{
		return 1;
	}
	return 0;
}
//
static int _read(unsigned char *buffer,int len)
{
	return 0;
}
//
static int _write(unsigned char *buffer,int len)
{
	switch(len)
	{
		case NNUM:
			break;
		case KNUM:
			oled_show_t.show_refresh_num=KNUM;
			break;
		case KLNUM:
			oled_show_t.show_refresh_num=KLNUM;
			break;
		case HNUM:
			oled_show_t.show_refresh_num=HNUM;
			break;
		case SNUM:
			oled_show_t.show_refresh_num=SNUM;
			break;
		case ILNUM:
			oled_show_t.show_refresh_num=ILNUM;
			break;
		case SENNUM:
			oled_show_t.show_refresh_num=SENNUM;
			break;
		case STRNUM:	
			oled_show_t.show_refresh_num=STRNUM;
			break;
		case SLNUM:
			oled_show_t.show_refresh_num=SLNUM;
			break;
		case MODENUM:
			oled_show_t.show_refresh_num=MODENUM;
			break;
		case MODECLO:
			oled_show_t.show_refresh_num=MODECLO;		//清屏	
			break;
		case VOLNUM:
			oled_show_t.show_refresh_num=VOLNUM;
			break;
		case CHARNUM:
			oled_show_t.show_refresh_num=CHARNUM;
			break;
		default:
			break;
	}
	return 0;
}
//
static void _handler(void *event)
{
	if(show_tick1ms)
	{
		show_tick1ms=0;
		if(oled_show_t.status==SHOW_BMP)
		{
			oled_show_t.logo_cnt++;
		}
		else
			oled_show_t.logo_cnt=0;
		show_cnt++;
	}
	if(show_cnt>=SHOW_CNT)
	{
		show_cnt=0;
		
		if(device_t.status.work_sta!=STATUS_OFF)//==STATUS_WORK)
		{
			switch(oled_show_t.status)
			{
				case SHOW_NULL:					
					OLED_DrawBMP(0, 0, 128, 8, logo);
					oled_show_t.status=SHOW_BMP;
					show_cnt=SHOW_CNT-1;					
					break;
				case SHOW_BMP:									
					if(oled_show_t.logo_cnt>2000)
					{	
						oled_show_t.logo_cnt=0;
						OLED_Clear(0,8);					
						show_cnt=SHOW_CNT-1;
						device_t.status.pwm_sta=1;
						oled_show_t.status=SHOW_PARAM;
					}
					break;
				case SHOW_PARAM:
					oled_show_param();
					oled_show_t.status=SHOW_CHANGE;
					break;
				case SHOW_CHANGE:
					oled_show_change(oled_show_t.show_refresh_num);			
					break;
				case SHOW_VER:					
					;
					break;
				default:
					break;
			}
		}
		else	// if(device_t.status.work_sta==STATUS_OFF)		
		{
			oled_show_change(NNUM);
		}
	}
}
//
const struct driver_interface_t oled_interface =
{
	_get,_set,_init,_status,_read,_write,_handler,
};

